1   /*
2    * Copyright (C) 2008 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.primitives;
18  
19  import com.google.common.annotations.GwtCompatible;
20  import com.google.common.base.Converter;
21  import com.google.common.collect.testing.Helpers;
22  
23  import junit.framework.TestCase;
24  
25  import java.util.Arrays;
26  import java.util.Collection;
27  import java.util.Collections;
28  import java.util.Comparator;
29  import java.util.List;
30  
31  /**
32   * Unit test for {@link Shorts}.
33   *
34   * @author Kevin Bourrillion
35   */
36  @GwtCompatible(emulated = true)
37  @SuppressWarnings("cast") // redundant casts are intentional and harmless
38  public class ShortsTest extends TestCase {
39    private static final short[] EMPTY = {};
40    private static final short[] ARRAY1 = {(short) 1};
41    private static final short[] ARRAY234
42        = {(short) 2, (short) 3, (short) 4};
43  
44    private static final short LEAST = Short.MIN_VALUE;
45    private static final short GREATEST = Short.MAX_VALUE;
46  
47    private static final short[] VALUES =
48        { LEAST, (short) -1, (short) 0, (short) 1, GREATEST };
49  
50    public void testHashCode() {
51      for (short value : VALUES) {
52        assertEquals(((Short) value).hashCode(), Shorts.hashCode(value));
53      }
54    }
55  
56    public void testCheckedCast() {
57      for (short value : VALUES) {
58        assertEquals(value, Shorts.checkedCast((long) value));
59      }
60      assertCastFails(GREATEST + 1L);
61      assertCastFails(LEAST - 1L);
62      assertCastFails(Long.MAX_VALUE);
63      assertCastFails(Long.MIN_VALUE);
64    }
65  
66    public void testSaturatedCast() {
67      for (short value : VALUES) {
68        assertEquals(value, Shorts.saturatedCast((long) value));
69      }
70      assertEquals(GREATEST, Shorts.saturatedCast(GREATEST + 1L));
71      assertEquals(LEAST, Shorts.saturatedCast(LEAST - 1L));
72      assertEquals(GREATEST, Shorts.saturatedCast(Long.MAX_VALUE));
73      assertEquals(LEAST, Shorts.saturatedCast(Long.MIN_VALUE));
74    }
75  
76    private static void assertCastFails(long value) {
77      try {
78        Shorts.checkedCast(value);
79        fail("Cast to short should have failed: " + value);
80      } catch (IllegalArgumentException ex) {
81        assertTrue(value + " not found in exception text: " + ex.getMessage(),
82            ex.getMessage().contains(String.valueOf(value)));
83      }
84    }
85  
86    public void testCompare() {
87      for (short x : VALUES) {
88        for (short y : VALUES) {
89          // Only compare the sign of the result of compareTo().
90          int expected = Short.valueOf(x).compareTo(y);
91          int actual = Shorts.compare(x, y);
92          if (expected == 0) {
93            assertEquals(x + ", " + y, expected, actual);
94          } else if (expected < 0) {
95            assertTrue(x + ", " + y + " (expected: " + expected + ", actual" + actual + ")",
96                actual < 0);
97          } else {
98            assertTrue(x + ", " + y + " (expected: " + expected + ", actual" + actual + ")",
99                actual > 0);
100         }
101       }
102     }
103   }
104 
105   public void testContains() {
106     assertFalse(Shorts.contains(EMPTY, (short) 1));
107     assertFalse(Shorts.contains(ARRAY1, (short) 2));
108     assertFalse(Shorts.contains(ARRAY234, (short) 1));
109     assertTrue(Shorts.contains(new short[] {(short) -1}, (short) -1));
110     assertTrue(Shorts.contains(ARRAY234, (short) 2));
111     assertTrue(Shorts.contains(ARRAY234, (short) 3));
112     assertTrue(Shorts.contains(ARRAY234, (short) 4));
113   }
114 
115   public void testIndexOf() {
116     assertEquals(-1, Shorts.indexOf(EMPTY, (short) 1));
117     assertEquals(-1, Shorts.indexOf(ARRAY1, (short) 2));
118     assertEquals(-1, Shorts.indexOf(ARRAY234, (short) 1));
119     assertEquals(0, Shorts.indexOf(
120         new short[] {(short) -1}, (short) -1));
121     assertEquals(0, Shorts.indexOf(ARRAY234, (short) 2));
122     assertEquals(1, Shorts.indexOf(ARRAY234, (short) 3));
123     assertEquals(2, Shorts.indexOf(ARRAY234, (short) 4));
124     assertEquals(1, Shorts.indexOf(
125         new short[] { (short) 2, (short) 3, (short) 2, (short) 3 },
126         (short) 3));
127   }
128 
129   public void testIndexOf_arrayTarget() {
130     assertEquals(0, Shorts.indexOf(EMPTY, EMPTY));
131     assertEquals(0, Shorts.indexOf(ARRAY234, EMPTY));
132     assertEquals(-1, Shorts.indexOf(EMPTY, ARRAY234));
133     assertEquals(-1, Shorts.indexOf(ARRAY234, ARRAY1));
134     assertEquals(-1, Shorts.indexOf(ARRAY1, ARRAY234));
135     assertEquals(0, Shorts.indexOf(ARRAY1, ARRAY1));
136     assertEquals(0, Shorts.indexOf(ARRAY234, ARRAY234));
137     assertEquals(0, Shorts.indexOf(
138         ARRAY234, new short[] { (short) 2, (short) 3 }));
139     assertEquals(1, Shorts.indexOf(
140         ARRAY234, new short[] { (short) 3, (short) 4 }));
141     assertEquals(1, Shorts.indexOf(ARRAY234, new short[] { (short) 3 }));
142     assertEquals(2, Shorts.indexOf(ARRAY234, new short[] { (short) 4 }));
143     assertEquals(1, Shorts.indexOf(new short[] { (short) 2, (short) 3,
144         (short) 3, (short) 3, (short) 3 },
145         new short[] { (short) 3 }
146     ));
147     assertEquals(2, Shorts.indexOf(
148         new short[] { (short) 2, (short) 3, (short) 2,
149             (short) 3, (short) 4, (short) 2, (short) 3},
150         new short[] { (short) 2, (short) 3, (short) 4}
151     ));
152     assertEquals(1, Shorts.indexOf(
153         new short[] { (short) 2, (short) 2, (short) 3,
154             (short) 4, (short) 2, (short) 3, (short) 4},
155         new short[] { (short) 2, (short) 3, (short) 4}
156     ));
157     assertEquals(-1, Shorts.indexOf(
158         new short[] { (short) 4, (short) 3, (short) 2},
159         new short[] { (short) 2, (short) 3, (short) 4}
160     ));
161   }
162 
163   public void testLastIndexOf() {
164     assertEquals(-1, Shorts.lastIndexOf(EMPTY, (short) 1));
165     assertEquals(-1, Shorts.lastIndexOf(ARRAY1, (short) 2));
166     assertEquals(-1, Shorts.lastIndexOf(ARRAY234, (short) 1));
167     assertEquals(0, Shorts.lastIndexOf(
168         new short[] {(short) -1}, (short) -1));
169     assertEquals(0, Shorts.lastIndexOf(ARRAY234, (short) 2));
170     assertEquals(1, Shorts.lastIndexOf(ARRAY234, (short) 3));
171     assertEquals(2, Shorts.lastIndexOf(ARRAY234, (short) 4));
172     assertEquals(3, Shorts.lastIndexOf(
173         new short[] { (short) 2, (short) 3, (short) 2, (short) 3 },
174         (short) 3));
175   }
176 
177   public void testMax_noArgs() {
178     try {
179       Shorts.max();
180       fail();
181     } catch (IllegalArgumentException expected) {
182     }
183   }
184 
185   public void testMax() {
186     assertEquals(LEAST, Shorts.max(LEAST));
187     assertEquals(GREATEST, Shorts.max(GREATEST));
188     assertEquals((short) 9, Shorts.max(
189         (short) 8, (short) 6, (short) 7,
190         (short) 5, (short) 3, (short) 0, (short) 9));
191   }
192 
193   public void testMin_noArgs() {
194     try {
195       Shorts.min();
196       fail();
197     } catch (IllegalArgumentException expected) {
198     }
199   }
200 
201   public void testMin() {
202     assertEquals(LEAST, Shorts.min(LEAST));
203     assertEquals(GREATEST, Shorts.min(GREATEST));
204     assertEquals((short) 0, Shorts.min(
205         (short) 8, (short) 6, (short) 7,
206         (short) 5, (short) 3, (short) 0, (short) 9));
207   }
208 
209   public void testConcat() {
210     assertTrue(Arrays.equals(EMPTY, Shorts.concat()));
211     assertTrue(Arrays.equals(EMPTY, Shorts.concat(EMPTY)));
212     assertTrue(Arrays.equals(EMPTY, Shorts.concat(EMPTY, EMPTY, EMPTY)));
213     assertTrue(Arrays.equals(ARRAY1, Shorts.concat(ARRAY1)));
214     assertNotSame(ARRAY1, Shorts.concat(ARRAY1));
215     assertTrue(Arrays.equals(ARRAY1, Shorts.concat(EMPTY, ARRAY1, EMPTY)));
216     assertTrue(Arrays.equals(
217         new short[] {(short) 1, (short) 1, (short) 1},
218         Shorts.concat(ARRAY1, ARRAY1, ARRAY1)));
219     assertTrue(Arrays.equals(
220         new short[] {(short) 1, (short) 2, (short) 3, (short) 4},
221         Shorts.concat(ARRAY1, ARRAY234)));
222   }
223 
224   public void testEnsureCapacity() {
225     assertSame(EMPTY, Shorts.ensureCapacity(EMPTY, 0, 1));
226     assertSame(ARRAY1, Shorts.ensureCapacity(ARRAY1, 0, 1));
227     assertSame(ARRAY1, Shorts.ensureCapacity(ARRAY1, 1, 1));
228     assertTrue(Arrays.equals(
229         new short[] {(short) 1, (short) 0, (short) 0},
230         Shorts.ensureCapacity(ARRAY1, 2, 1)));
231   }
232 
233   public void testEnsureCapacity_fail() {
234     try {
235       Shorts.ensureCapacity(ARRAY1, -1, 1);
236       fail();
237     } catch (IllegalArgumentException expected) {
238     }
239     try {
240       // notice that this should even fail when no growth was needed
241       Shorts.ensureCapacity(ARRAY1, 1, -1);
242       fail();
243     } catch (IllegalArgumentException expected) {
244     }
245   }
246 
247   public void testJoin() {
248     assertEquals("", Shorts.join(",", EMPTY));
249     assertEquals("1", Shorts.join(",", ARRAY1));
250     assertEquals("1,2", Shorts.join(",", (short) 1, (short) 2));
251     assertEquals("123",
252         Shorts.join("", (short) 1, (short) 2, (short) 3));
253   }
254 
255   public void testLexicographicalComparator() {
256     List<short[]> ordered = Arrays.asList(
257         new short[] {},
258         new short[] {LEAST},
259         new short[] {LEAST, LEAST},
260         new short[] {LEAST, (short) 1},
261         new short[] {(short) 1},
262         new short[] {(short) 1, LEAST},
263         new short[] {GREATEST, GREATEST - (short) 1},
264         new short[] {GREATEST, GREATEST},
265         new short[] {GREATEST, GREATEST, GREATEST});
266 
267     Comparator<short[]> comparator = Shorts.lexicographicalComparator();
268     Helpers.testComparator(comparator, ordered);
269   }
270 
271   public void testToArray() {
272     // need explicit type parameter to avoid javac warning!?
273     List<Short> none = Arrays.<Short>asList();
274     assertTrue(Arrays.equals(EMPTY, Shorts.toArray(none)));
275 
276     List<Short> one = Arrays.asList((short) 1);
277     assertTrue(Arrays.equals(ARRAY1, Shorts.toArray(one)));
278 
279     short[] array = {(short) 0, (short) 1, (short) 3};
280 
281     List<Short> three = Arrays.asList((short) 0, (short) 1, (short) 3);
282     assertTrue(Arrays.equals(array, Shorts.toArray(three)));
283 
284     assertTrue(Arrays.equals(array, Shorts.toArray(Shorts.asList(array))));
285   }
286 
287   public void testToArray_threadSafe() {
288     for (int delta : new int[] { +1, 0, -1 }) {
289       for (int i = 0; i < VALUES.length; i++) {
290         List<Short> list = Shorts.asList(VALUES).subList(0, i);
291         Collection<Short> misleadingSize =
292             Helpers.misleadingSizeCollection(delta);
293         misleadingSize.addAll(list);
294         short[] arr = Shorts.toArray(misleadingSize);
295         assertEquals(i, arr.length);
296         for (int j = 0; j < i; j++) {
297           assertEquals(VALUES[j], arr[j]);
298         }
299       }
300     }
301   }
302 
303   public void testToArray_withNull() {
304     List<Short> list = Arrays.asList((short) 0, (short) 1, null);
305     try {
306       Shorts.toArray(list);
307       fail();
308     } catch (NullPointerException expected) {
309     }
310   }
311 
312   public void testToArray_withConversion() {
313     short[] array = {(short) 0, (short) 1, (short) 2};
314 
315     List<Byte> bytes = Arrays.asList((byte) 0, (byte) 1, (byte) 2);
316     List<Short> shorts = Arrays.asList((short) 0, (short) 1, (short) 2);
317     List<Integer> ints = Arrays.asList(0, 1, 2);
318     List<Float> floats = Arrays.asList((float) 0, (float) 1, (float) 2);
319     List<Long> longs = Arrays.asList((long) 0, (long) 1, (long) 2);
320     List<Double> doubles = Arrays.asList((double) 0, (double) 1, (double) 2);
321 
322     assertTrue(Arrays.equals(array, Shorts.toArray(bytes)));
323     assertTrue(Arrays.equals(array, Shorts.toArray(shorts)));
324     assertTrue(Arrays.equals(array, Shorts.toArray(ints)));
325     assertTrue(Arrays.equals(array, Shorts.toArray(floats)));
326     assertTrue(Arrays.equals(array, Shorts.toArray(longs)));
327     assertTrue(Arrays.equals(array, Shorts.toArray(doubles)));
328   }
329 
330   public void testAsList_isAView() {
331     short[] array = {(short) 0, (short) 1};
332     List<Short> list = Shorts.asList(array);
333     list.set(0, (short) 2);
334     assertTrue(Arrays.equals(new short[] {(short) 2, (short) 1}, array));
335     array[1] = (short) 3;
336     assertEquals(Arrays.asList((short) 2, (short) 3), list);
337   }
338 
339   public void testAsList_toArray_roundTrip() {
340     short[] array = { (short) 0, (short) 1, (short) 2 };
341     List<Short> list = Shorts.asList(array);
342     short[] newArray = Shorts.toArray(list);
343 
344     // Make sure it returned a copy
345     list.set(0, (short) 4);
346     assertTrue(Arrays.equals(
347         new short[] { (short) 0, (short) 1, (short) 2 }, newArray));
348     newArray[1] = (short) 5;
349     assertEquals((short) 1, (short) list.get(1));
350   }
351 
352   // This test stems from a real bug found by andrewk
353   public void testAsList_subList_toArray_roundTrip() {
354     short[] array = { (short) 0, (short) 1, (short) 2, (short) 3 };
355     List<Short> list = Shorts.asList(array);
356     assertTrue(Arrays.equals(new short[] { (short) 1, (short) 2 },
357         Shorts.toArray(list.subList(1, 3))));
358     assertTrue(Arrays.equals(new short[] {},
359         Shorts.toArray(list.subList(2, 2))));
360   }
361 
362   public void testAsListEmpty() {
363     assertSame(Collections.emptyList(), Shorts.asList(EMPTY));
364   }
365 
366   public void testStringConverter_convert() {
367     Converter<String, Short> converter = Shorts.stringConverter();
368     assertEquals((Short) (short) 1, converter.convert("1"));
369     assertEquals((Short) (short) 0, converter.convert("0"));
370     assertEquals((Short) (short) (-1), converter.convert("-1"));
371     assertEquals((Short) (short) 255, converter.convert("0xff"));
372     assertEquals((Short) (short) 255, converter.convert("0xFF"));
373     assertEquals((Short) (short) (-255), converter.convert("-0xFF"));
374     assertEquals((Short) (short) 255, converter.convert("#0000FF"));
375     assertEquals((Short) (short) 438, converter.convert("0666"));
376   }
377 
378   public void testStringConverter_convertError() {
379     try {
380       Shorts.stringConverter().convert("notanumber");
381       fail();
382     } catch (NumberFormatException expected) {
383     }
384   }
385 
386   public void testStringConverter_nullConversions() {
387     assertNull(Shorts.stringConverter().convert(null));
388     assertNull(Shorts.stringConverter().reverse().convert(null));
389   }
390 
391   public void testStringConverter_reverse() {
392     Converter<String, Short> converter = Shorts.stringConverter();
393     assertEquals("1", converter.reverse().convert((short) 1));
394     assertEquals("0", converter.reverse().convert((short) 0));
395     assertEquals("-1", converter.reverse().convert((short) -1));
396     assertEquals("255", converter.reverse().convert((short) 0xff));
397     assertEquals("255", converter.reverse().convert((short) 0xFF));
398     assertEquals("-255", converter.reverse().convert((short) -0xFF));
399     assertEquals("438", converter.reverse().convert((short) 0666));
400   }
401 }
402